package com.example.test;


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.test.bean.Course;

public class AddCourseActivity extends AppCompatActivity {

    private EditText etCourseName, etTeacherName, etClassTime, etClassLocation;
    private Button btnAddCourse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_course);

        //获取组件
        etCourseName = findViewById(R.id.et_course_name);
        etTeacherName = findViewById(R.id.et_teacher_name);
        etClassTime = findViewById(R.id.et_class_time);
        etClassLocation = findViewById(R.id.et_class_location);
        btnAddCourse = findViewById(R.id.btn_add_course);

        btnAddCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取输入的课程信息
                String courseName = etCourseName.getText().toString().trim();
                String teacherName = etTeacherName.getText().toString().trim();
                String classTime = etClassTime.getText().toString().trim();
                String classLocation = etClassLocation.getText().toString().trim();

                // 检查输入是否为空
                if (TextUtils.isEmpty(courseName) || TextUtils.isEmpty(teacherName) ||
                        TextUtils.isEmpty(classTime) || TextUtils.isEmpty(classLocation)) {
                    Toast.makeText(AddCourseActivity.this, "请输入完整的课程信息", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 创建课程对象
                Course course = new Course(1, courseName, teacherName, classTime, classLocation);

                DatabaseOpenHelper  dbHelper = new DatabaseOpenHelper(AddCourseActivity.this);
                dbHelper.addCourse(course);
                Toast.makeText(AddCourseActivity.this, "课程添加成功", Toast.LENGTH_SHORT).show();
                finish(); // 结束当前 Activity 返回上一个界面
            }
        });
    }
}

